import 'package:flutter/material.dart';
import 'package:gzmall_app/common/api.dart';
import 'package:gzmall_app/common/net_util.dart';
import 'package:gzmall_app/common/refresh_listview.dart';
import 'package:gzmall_app/common/toast_util.dart';
import 'package:gzmall_app/goods/goods_details_page.dart';
import 'package:gzmall_app/goods/goods_list_item_widget.dart';
import 'package:gzmall_app/model/product.dart';
import 'product_item_widget.dart';

class ClearanceListWidget extends StatefulWidget {
  int id;

  ClearanceListWidget(this.id);

  @override
  State<StatefulWidget> createState() => ClearanceListWidgetState(this.id);
}

class ClearanceListWidgetState extends State<ClearanceListWidget> {
  List<Product> _clearanceList = [];
  int id;

  double _itemWidth;

  int _curPage = 1;
  RefreshListView _refreshListView;

  ClearanceListWidgetState(this.id);

  void init() {
    var size = MediaQuery.of(context).size;
    _itemWidth = (size.width - 12) / 2;
  }

  @override
  void initState() {
    super.initState();
    _getClearance(id, 1);
  }

  @override
  Widget build(BuildContext context) {
    init();
    return _buildGirdList();
  }

  Widget _buildGirdList() {
    List<Widget> clearanceWs = _clearanceList
        .map((item) => InkWell(
              child: GoodsListItemWidget(item, _itemWidth),
              onTap: () {
                if (item.id != null) {
                  Navigator.of(context)
                      .push(new MaterialPageRoute(builder: (_) {
                    return new GoodsDetailsPage(productId: item.id);
                  }));
                }
              },
            ))
        .toList();
    _refreshListView = RefreshListView(
      enablePullUp: true,
      enablePullDown: true,
      onRefresh: _onRefresh,
      child: GridView.count(
        shrinkWrap: true,
        padding: const EdgeInsets.only(left: 4.0, right: 4.0, top: 4.0),
        crossAxisSpacing: 4.0,
        mainAxisSpacing: 4.0,
        controller: new ScrollController(keepScrollOffset: false),
        crossAxisCount: 2,
        childAspectRatio: 0.65,
        children: clearanceWs,
      ),
    );
    return Container(color: Colors.white, child: _refreshListView);
  }

  void _onRefresh(bool up) {
    if (up) {
      _getClearance(id, 1);
    } else {
      _getClearance(id, _curPage + 1);
    }
  }

  _getClearance(int shopCategoryId, int page) {
    bool isGetNew = page == 1;
    Map<String, String> params = {
      "page": page.toString(),
      "shop_category_id": shopCategoryId.toString(),
    };
    NetUtil.get(
        context,
        Api.pagesClearance,
        (data, message) {
          if (!mounted) return;
          _curPage = page;
          var _dataList = (data as List)
              ?.map((e) => e == null ? null : Product.fromJson(e))
              ?.toList();
          setState(() {
            if (isGetNew) {
              _clearanceList = _dataList;
            } else {
              _clearanceList.addAll(_dataList);
            }
          });
          if (_refreshListView != null) {
            _refreshListView.refreshCompleted(isGetNew,
                hasMore: _dataList != null && _dataList.length >= Api.pageSize);
          }
        },
        params: params,
        errorCallBack: (e) {
          if (_refreshListView != null) {
            _refreshListView.refreshCompleted(isGetNew);
          }
        });
  }
}
